<?php
    session_start();    
    require_once "../funciones.php";
    $link_caweb = con_caweb();

    $op=$_REQUEST[op];
    
    $invexcluir=$_REQUEST[invexcluir];
    $listabodegasex =  split(',',$invexcluir);
    
    if($op=='Equipos') { $tablabk='seriales_invefisico_bk';         $tabla='seriales_invefisico';          }
    if($op=='Partes')  { $tablabk='seriales_partes_invefisico_bk';  $tabla='seriales_partes_invefisico';   }
    if($op=='Kardex')  { $tablabk='kardex_invefisico_bk';           $tabla='kardex_invefisico';            }
    
    echo "<font size=4>Invefisico de </font><font color='blue'>".$op."</font>";
    $result ="SELECT count(*) AS cantidad,fechabk FROM $tablabk WHERE bodega NOT IN (";
        foreach ($listabodegasex as $value) { $result.="'$value',";  } $result = substr ($result, 0, strlen($result) - 1);
    $result .=")";
    
 	$result=mysql_query($result,$link_caweb) or die('Error1 $tablabk, '.mysql_error());
    $row = mysql_fetch_array($result);
    echo "&nbsp;&nbsp;&nbsp;Elementos: <font color='blue'>$row[cantidad] </font>";
    echo "&nbsp;&nbsp;&nbsp;Backup del : <font color='blue'>$row[fechabk] </font>";
    echo "&nbsp;&nbsp;&nbsp;Exluido : <font color='blue'>$invexcluir </font>";
    echo "<hr>";
    
    $opcion=0;
    $result0 ="SELECT codigosucursal,nombre FROM sucursales";
    $result0=mysql_query($result0,$link_caweb) or die('Error2 sucursales, '.mysql_error());
    while($row0 = mysql_fetch_array($result0))
    {
        echo "Sucursal: $row0[codigosucursal] - $row0[nombre]";
        $result ="SELECT * FROM $tablabk WHERE bodega NOT IN (";
        foreach ($listabodegasex as $value) { $result.="'$value',";  } $result = substr ($result, 0, strlen($result) - 1);
        $result .=") GROUP BY bodega";
        ?>
        <table border="1"><tr bgcolor="aabbcc"><td>Bodega</td><td>Cant. Logica</td><td>Cant. Fisica</td><td>Ok</td><td>Reubicar</td><td>Nuevos</td><td>Faltantes</td></tr>
        <?
        
     	$result=mysql_query($result,$link_caweb) or die('Error3 $tablabk, '.mysql_error());
        while($row = mysql_fetch_array($result))
        {
            // Cantidad  logica
            $result2 ="SELECT count(*) AS cantidad FROM $tablabk WHERE bodega='$row[bodega]' AND sucursal='$row0[codigosucursal]'";
            $opcion++;
            $opcion2="verinvefisico".$opcion;
            $_SESSION['caweb'][$opcion2] = str_replace ("count(*) AS cantidad", " * ",$result2);
            $result2=mysql_query($result2,$link_caweb) or die('Error4 $tablabk, '.mysql_error());
            $row2 = mysql_fetch_array($result2);
            
            // Cantidad  fisica
            $result3 ="SELECT count(*) AS cantidad FROM $tabla WHERE bodega_f='$row[bodega]' AND sucursal_f='$row0[codigosucursal]'";
            $opcion++;
            $opcion2="verinvefisico".$opcion;
            $_SESSION['caweb'][$opcion2] = str_replace ("count(*) AS cantidad", " * ",$result3);
            $result3=mysql_query($result3,$link_caweb) or die('Error5 $tabla, '.mysql_error());
            $row3 = mysql_fetch_array($result3);
            
            // Ok
            $result4 ="
                SELECT count(*) AS cantidad 
                FROM $tablabk AS a
                LEFT JOIN $tabla AS b
                ON b.interno = a.interno
                WHERE a.bodega='$row[bodega]' AND a.bodega=b.bodega AND sucursal='$row0[codigosucursal]'";
            $opcion++;
            $opcion2="verinvefisico".$opcion;
            $_SESSION['caweb'][$opcion2] = str_replace ("count(*) AS cantidad", " * ",$result4);    
            $result4=mysql_query($result4,$link_caweb) or die('Error6 $tabla, '.mysql_error());
            $row4 = mysql_fetch_array($result4);

            
            // Reubicar
            $result5 ="
                SELECT count(*) AS cantidad 
                FROM $tablabk AS a
                LEFT JOIN $tabla AS b
                ON b.interno = a.interno
                WHERE a.bodega='$row[bodega]' AND a.bodega<>b.bodega AND sucursal='$row0[codigosucursal]'";
            $opcion++;
            $opcion2="verinvefisico".$opcion;
            $_SESSION['caweb'][$opcion2] = "
                SELECT a.interno,a.item,a.serial,a.sucursal AS sucursal_logica,a.bodega AS bodega_logica,b.sucursal AS sucursal_fisica,b.bodega AS bodega_fisica 
                FROM $tablabk AS a
                LEFT JOIN $tabla AS b
                ON b.interno = a.interno
                WHERE a.bodega='$row[bodega]' AND a.bodega<>b.bodega AND sucursal='$row0[codigosucursal]'";    
            $result5=mysql_query($result5,$link_caweb) or die('Error7 $tabla, '.mysql_error());
            $row5 = mysql_fetch_array($result5);

            // Nuevos
            $result6 ="
                SELECT count(*) AS cantidad 
                FROM $tabla AS a
                LEFT JOIN $tablabk AS b 
                ON b.interno = a.interno
                WHERE a.bodega_f = '$row[bodega]' AND a.sucursal_f = '$row0[codigosucursal]' AND b.bodega IS NULL";
            $opcion++;
            $opcion2="verinvefisico".$opcion;
            $_SESSION['caweb'][$opcion2] = str_replace ("count(*) AS cantidad", " * ",$result6);    
            $result6=mysql_query($result6,$link_caweb) or die('Error8 $tabla, '.mysql_error());
            $row6 = mysql_fetch_array($result6);
            
            // Faltantes
            $result7 ="
                SELECT count(*) AS cantidad 
                FROM $tablabk AS a
                LEFT JOIN $tabla AS b
                ON b.interno = a.interno
                WHERE a.bodega='$row[bodega]' AND a.sucursal='$row0[codigosucursal]' AND b.bodega_f IS NULL";
            $opcion++;
            $opcion2="verinvefisico".$opcion;
            $_SESSION['caweb'][$opcion2] = "SELECT a.interno,a.item,a.serial,a.sucursal AS sucursal,a.bodega
                FROM $tablabk AS a
                LEFT JOIN $tabla AS b
                ON b.interno = a.interno
                WHERE a.bodega='$row[bodega]' AND a.sucursal='$row0[codigosucursal]' AND b.bodega_f IS NULL";    
            $result7=mysql_query($result7,$link_caweb) or die('Error9 $tabla, '.mysql_error());
            $row7 = mysql_fetch_array($result7);
       
           
           $opcion=$opcion-5;
            echo "<tr><td>$row[bodega]<td>
            <a href=verinvefisico.php?opcion=verinvefisico".$opcion++.">".number_format($row2[cantidad],0, ',', '.')."</a><td>
            <a href=verinvefisico.php?opcion=verinvefisico".$opcion++.">".number_format($row3[cantidad],0, ',', '.')."</a><td>
            <a href=verinvefisico.php?opcion=verinvefisico".$opcion++.">".number_format($row4[cantidad],0, ',', '.')."</a><td>
            <a href=verinvefisico.php?opcion=verinvefisico".$opcion++.">".number_format($row5[cantidad],0, ',', '.')."</a><td>
            <a href=verinvefisico.php?opcion=verinvefisico".$opcion++.">".number_format($row6[cantidad],0, ',', '.')."</a><td>
            <a href=verinvefisico.php?opcion=verinvefisico".$opcion.">".number_format($row7[cantidad],0, ',', '.')."</a><td>
            </tr>";
        }
        ?>
        </table>
    <?
    }

?>